package demo3;



/*@FunctionalInterface
interface NoParameterNoReturn {
    //注意：只能有一个方法
    void test();

    default void test2() {
        System.out.println("JDK1.8新特性，default默认方法可以有具体的实现");
    }
    static void test3() {
        System.out.println("静态方法");
    }
}*/


import java.util.*;
import java.util.Comparator;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

//无返回值无参数
@FunctionalInterface
interface NoParameterNoReturn {
    void test();
}

//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
    void test(int a);
}

//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
    void test(int a, int b);
}

//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
    int test();
}

//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
    int test(int a);
}


//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a, int b);
}


public class Test {

    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "hello");
        map.put(2, "bit");
        map.put(3, "hello");
        map.put(4, "lambda");

        map.forEach(new BiConsumer<Integer, String>(){
            @Override
            public void accept(Integer integer, String s){
                System.out.println("key: " + integer + " val: " + s);
            }
        });
        System.out.println("===============");
        //使用lambda表达式
        map.forEach((key, val) -> System.out.println("key: " + key + " val: " + val));

    }



    public static void main3(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("hello");
        list.add("bit");
        list.add("hello");
        list.add("lambda");

        /*list.forEach(new Consumer<String>() {
            @Override
            public void accept(String str) {
                //简单遍历集合中的元素。
                System.out.println(str);
            }
        });
        System.out.println("===============");
        list.forEach(str -> System.out.println(str));*/


        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });

        list.forEach(str -> System.out.println(str));

        System.out.println("===============");
        //使用lambda表达式
        list.sort((o1, o2) -> {return o1.compareTo(o2);});
        list.forEach(str -> System.out.println(str));
    }


    public static void main2(String[] args) {

        //无返回值无参数
        int size = 100;
        //size = 55;//error
        NoParameterNoReturn noParameterNoReturn = () -> System.out.println("无返回值无参数" + size);
        noParameterNoReturn.test();

        //无返回值一个参数
        OneParameterNoReturn oneParameterNoReturn = a -> System.out.println("无返回值有一个参数:" + a);
        oneParameterNoReturn.test(12);

        //无返回值多个参数
        MoreParameterNoReturn moreParameterNoReturn = (int a, int b) -> System.out.println("无返回值多个参数:" + (a + b));
        moreParameterNoReturn.test(6, 8);

        //有返回值无参数
        NoParameterReturn noParameterReturn = () -> {
            return 6;
        };
        System.out.println(noParameterReturn.test());

        //有返回值一个参数
        OneParameterReturn oneParameterReturn = (a) -> {
            return a;
        };
        System.out.println(oneParameterReturn.test(16));

        //有返回值多参数
        MoreParameterReturn moreParameterReturn = (a, b) -> {
            return a + b;
        };
        System.out.println(moreParameterReturn.test(15, 16));
    }

    public static void main1(String[] args) {
        int c = 10;

        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn() {
            @Override
            public void test() {
                //c = 99;
                System.out.println("重写了test方法！" + c);
            }
        };
        noParameterNoReturn.test();
    }

}
